#!/bin/bash
#
# Functions
#

abort() {
echo " ===> ERROR: $i"
exit 1
}

#
## Begin
#

if [ -z "$LOG" ]; then
    PASS=`pwd`
    PASS=`basename $PASS`
    LOG=$HOME/logs/$PASS
fi
if [ ! -d $LOG ]; then
	mkdir -p $LOG
fi

if [ ! -f .list ]; then
    LIST="$HOME/bin/.list"
else
	LIST=".list"
fi
for DIR in `cat $LIST`
do
    NAME=`basename $DIR`
    BUILD="yes"
    PKGMK="pkgmk.passes"
    if [ -f ${LOG}/$NAME.log ]; then
        if ( grep "^=======>" ${LOG}/$NAME.log|grep "succeeded"  > /dev/null); then
            BUILD="no"
        else
            BUILD="yes"
        fi
    fi
    if [ "$BUILD" == "yes" ]; then
        cd $DIR
        case $PASS in
            cross)
                PKGMK="pkgmk.passes"
                $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1;;
            tools)
                export CC="${CLFS_TARGET}-gcc"
                export CXX="${CLFS_TARGET}-g++"
                export AR="${CLFS_TARGET}-ar"
                export AS="${CLFS_TARGET}-as"
                export RANLIB="${CLFS_TARGET}-ranlib"
                export LD="${CLFS_TARGET}-ld"
                export STRIP="${CLFS_TARGET}-strip"
                PKGMK="pkgmk.passes"
                $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1;;
            chroot)
                PKGMK="pkgmk.passes"
                $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1;;
            base-arm)
                PKGMK="pkgmk";;
            base)
                PKGMK="pkgmk"
                case $NAME in
		    glibc)
			mkdir -pv /usr/include
			$PKGMK -cf ../../../bin/pkgmk.conf.passes -i -kw 2>&1|tee ${LOG}/$NAME.log || exit 1
			;;
                    gcc)
                        $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1
                        rm -fv /usr/lib{,64}/libgcc_s.so{,.1}
                        rm -fv /usr/lib{,64}/libstdc++.so{,.6}
                        rm -fv /usr/lib{,64}/libstdc++.la
                        $PKGMK -d -cf ../../../bin/pkgmk.conf.passes -i || exit 1
                        for i in *cards.tar*
                        do
                            echo "=======> Installing '$i' succeeded." >> ${LOG}/$NAME.log
                        done
                        ;;
                    bash)
                        $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1
                        rm /bin/bash
                        rm /bin/sh
                        echo "need to install bash manually"
                        echo "=======> Installing '$NAME' succeeded." >> ${LOG}/$NAME.log
                        exit 1
                        $PKGMK -d -cf ../../../bin/pkgmk.conf.passes -i
                        ;;
                    coreutils)
                        $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1
                        rm -f /usr/bin/env
                        for i in cat echo pwd stty
                        do
                            rm -f /bin/$i
                        done
                        $PKGMK -d -cf ../../../bin/pkgmk.conf.passes -i || exit 1
                        for i in *cards.tar*
                        do
                            echo "=======> Installing '$i' succeeded." >> ${LOG}/$NAME.log
                        done
                        ;;

                    perl)
                        $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1
                        rm -f /usr/bin/perl
                        $PKGMK -d -cf ../../../bin/pkgmk.conf.passes -i || exit 1
                        for i in *cards.tar*
                        do
                            echo "=======> Installing '$i' succeeded." >> ${LOG}/$NAME.log
                        done
                        ;;

                   cards)
                        $PKGMK -cf ../../../bin/pkgmk.conf.passes -kw 2>&1|tee ${LOG}/$NAME.log || exit 1
                        rm -f /var/lib/pkg/pkgadd.conf
                        $PKGMK -d -cf ../../../bin/pkgmk.conf.passes -i || exit 1
                        for i in *cards.tar*
                        do
                            echo "=======> Installing '$i' succeeded." >> ${LOG}/$NAME.log
                        done
                        ;;
                    *)
                        $PKGMK -cf ../../../bin/pkgmk.conf.passes -i -kw 2>&1|tee ${LOG}/$NAME.log || exit 1
                        ;;
                esac;;
            *)
                PKGMK="pkgmk -d -i"
                $PKGMK  2>&1|tee ${LOG}/$NAME.log || exit 1;;
        esac
	if ( grep "^=======>" ${LOG}/$NAME.log|grep "succeeded"  > /dev/null); then
	    echo "done"
	else
	    exit 1
	fi
        if ( grep "^=======>" ${LOG}/$NAME.log| tail -1 |grep "ERROR"  > /dev/null); then
            exit 1
        fi
        cd -
    fi
done
